using Sys = System;

namespace SharpMud.Server.Systems.Security.Entities.BuiltIn.Users
{
	public class System : SharpMud.Server.Systems.Security.Entities.IUser
	{
		private const string	_UserName	=	"System";
		private const string	_UserIDSeed	=	"{C8B42A47-C997-46d6-9A80-96A23A528570}";

		#region Private Variables
		
		private SecurityManager _SecurityManager;
		private Sys.Guid		_UserID;
		private string			_UnhashedPassword;
		private string			_HashedPassword;
		#endregion

		#region Constructors
		public System()
			:this(SecurityManager.GlobalInstance)
		{
		}
		public System(SecurityManager securityManager)
		{
			if(securityManager==null)
				throw new Sys.InvalidOperationException("There must be an instance of SharpMud.Server.Systems.SecurityManager available to this IUser");
			_SecurityManager = securityManager;
			this._UserID = Security.Utility.AddGuids(_SecurityManager.HostApp.___AppID
								,new Sys.Guid(_UserIDSeed));
			this._UnhashedPassword = Security.Utility.MakeSecureKey(this.ID,this.Name);
			this._HashedPassword = Security.Utility.DefaultTextEncoding.GetString(
					Security.Utility.DefaultHashAlgorithm.ComputeHash(
					Security.Utility.DefaultTextEncoding.GetBytes(this._UnhashedPassword)
					) );
		}
		#endregion

		#region IUser Members

		public bool HasPassword(string passwordGuess)
		{
			return (this._UnhashedPassword == passwordGuess);
		}

		public bool IsInRole(string roleName)
		{
			return this.IsInRole(new Security.Entities.UserRoleReference(roleName));
		}

		public bool IsInRole(IUserRole role)
		{
			return this.IsInRole(role.GetReference());
		}

		public bool IsInRole(UserRoleReference roleReference)
		{
			// TODO:  Add System.SharpMud.Server.Systems.Security.Entities.IUser.IsInRole implementation
			return false;
		}

		public UserReference GetReference()
		{
			return new UserReference(this);
		}

		#endregion

		#region IEntity Members

		public string Name
		{
			get
			{
				
				return System._UserName;
			}
		}

		public Sys.Guid ID
		{
			get
			{
				return this._UserID;
			}
		}

		EntityReference SharpMud.Server.Systems.Security.Entities.IEntity.GetReference()
		{
			return (EntityReference)this.GetReference();
		}

		#endregion
	}
}
